/**
 * The GoogleFeeds JavaScript library is used for processing and displaying
 * multiple RSS feeds using the Google AJAX Feed API.
 * Copyright (C) 2008-2010 Ramiro Gómez
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Ramiro Gómez www.ramiro.org
 * @see www.la-liga.eu for a usage example
 */
// load Google AJAX Feed API
google.load("feeds", "1");

// create an empty GoogleFeeds object
var GoogleFeeds = {
  /**
   * Set the element id of the feed container
   * @param {String} id Container element id
   */
  setContainerId: function(id) {
    GoogleFeeds.container_id = id;
  },
  /**
   * Set the feed URLs
   * @param {Array} feeds An array of feed URLs
   */
  setFeeds: function(feeds) {
    GoogleFeeds.feeds = feeds;
  },
  /**
   * Set the target container and process feeds
   */
  processFeeds: function() {
    GoogleFeeds.container = document.getElementById(GoogleFeeds.container_id);
    for (url in GoogleFeeds.feeds) {
      GoogleFeeds.displayFeed(GoogleFeeds.feeds[url]);
    }
  },
  /**
   * Display feed in the container element
   * @param {String} url a feed URL
   */
  displayFeed: function(url) {
    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(10);
    feed.load(function(result) {
      if (!result.error) {
        var div = document.createElement("div");
        div.className = 'block-feed';
        var heading = document.createElement("h2");
        var feed_title = result.feed.title;
        heading.appendChild(document.createTextNode(feed_title));
        div.appendChild(heading);
        var ul = document.createElement("ul");
        for (var i = 0; i < result.feed.entries.length; i++) {
          var entry = result.feed.entries[i];
          var link = '<a href="' + entry.link + '" title="' + entry.contentSnippet + '">'
            + entry.title + '</a>';
          var li = document.createElement("li");
          li.innerHTML = link;
          ul.appendChild(li);
        }
        div.appendChild(ul);
        var div_info = document.createElement("div");
        div_info.className = 'block-feed-info';
        var div_info_link = '<a href="' + url + '">'
          + '<img alt="RSS" src="images/rss.gif" title="Subscribir a: ' + feed_title + '"/></a>';
        div_info.innerHTML = div_info_link;
        div.appendChild(div_info);
        GoogleFeeds.container.appendChild(div);
      }
    });
  }
};

/**
 * escape HTML
 */
String.prototype.escapeHTML = function () {
  return (
    this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').
      replace(/</g,'&lt;').replace(/"/g,'&quot;')
  );
};

// initialize processing when document is fully loaded
google.setOnLoadCallback(GoogleFeeds.processFeeds);
